#include "Q7_4.h"

bool (*sortModeA)(float, float);

_StudentA::_StudentA(int id, char *name, float score)
{
	_StudentA::id = id;
	_StudentA::name = name;
	_StudentA::score = score;
}

void _StudentA::ShowA()
{
	printf("ID: %d\n", _StudentA::id);
	printf("Name: %s\n", _StudentA::name);
	printf("Score: %f\n\n", _StudentA::score);
}

bool AscA(float a, float b)
{
	return a > b;
}

bool DesA(float a, float b)
{
	return a < b;
}

void SwapA(StudentA &a, StudentA &b)
{
	int id = a.id;
	char *name = a.name;
	float score = a.score;

	a.id = b.id;
	a.name = b.name;
	a.score = b.score;

	b.id = id;
	b.name = name;
	b.score = score;
}

bool IsvalidA(StudentA room[], int n, StudentA student)
{
	if(student.score < 0 || student.score > 10)
		return false;

	bool flag = true;
	for(int i=0; i<n; i++)
	{
		if(room[i].id == student.id)
			flag = false;
	}
	return flag;
}

void SortStudentA(StudentA room[], int n, bool mode)
{	
	if(mode)
		sortModeA = AscA;
	else
		sortModeA = DesA;

	for(int i=0; i<n; i++)
		for(int j=i; j<n; j++)
			if(sortModeA(room[i].score, room[j].score))
				SwapA(room[i], room[j]);
}

void ShowA(StudentA room[], int n)
{
	for(int i=0; i<n; i++)
		room[i].ShowA();
}

void q7_4_run()
{
	int n= 10;

	StudentA *classroom = new StudentA[n];
	printf("Q7.4\n");
	printf("Input student information\n");

	for(int i=0; i<n; i++)
	{
		StudentA temp;
		do{
			cout << "Id: ";
			cin >> temp.id;
			cout << "Name: ";
			cin >> temp.name;
			cout << "Score: ";
			cin >> temp.score;
			cout<<endl;
			if(!IsvalidA(classroom, n, temp))
				cout<<"Mistack\n";
		}while(!IsvalidA(classroom, n, temp));
		classroom[i] = temp;
	}

	SortStudentA(classroom, n, true);

	ShowA(classroom, n);
}